﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace Simulador_Quadrotor {
    public class Serializador {
        #region Propiedades

        /// <summary>
        /// Direccion del archivo a serializar
        /// </summary>
        public string path { get; set; }

        #endregion

        #region Constructoras
        public Serializador(string path) {
            this.path = path;
        }

        #endregion

        #region Metodos

        public bool SerializarXML(Type tipoObjecto, object objeto) {
            StreamWriter streamWriter = null;
            try {
                XmlSerializer xmlSerializer = new XmlSerializer(tipoObjecto);
                streamWriter = new StreamWriter(this.path);
                xmlSerializer.Serialize(streamWriter, objeto);
                return true;

            }
            catch (Exception ex) {
                return false;
            }
            finally {
                if (streamWriter != null) {
                    streamWriter.Close();
                }
            }
        }

        public object DeserializarXML(Type tipoObjecto) {
            StreamReader streamReader = null;
            try {
                XmlSerializer xmlSerializer = new XmlSerializer(tipoObjecto);
                streamReader = new StreamReader(this.path);
                return xmlSerializer.Deserialize(streamReader);
            }
            catch (Exception ex) {
                return null;
            }

        }

        #endregion
    }
}
